# coding=utf-8
import json
import sys
import random
import string
from datetime import datetime
import threading
import time
import datetime
import pymysql

def conn_youhong(database):
    #连接数据库
    db = pymysql.connect("localhost", "root", "zhangkai", database)   # mysql 数据库
    # db = pymysql.connect("rm-bp1d39e7xi1g8p5i0.mysql.rds.aliyuncs.com", "dushiuser", "zajdkahdk1y23yiuy!@$!%$%1", "dushibaoliao")   # mysql 数据库
    return db

def exec_sql(sql, database, conn=None, close=True):
    """执行更新操作SQL"""
    if not sql:
        return
    if conn is None:
        conn = conn_youhong(database,)
    cxn = conn.cursor()
    cxn.execute(sql)
    conn.commit()
    cxn.close()
    if close:
        conn.close()


def exec_sql_statements(sql_statements, database, conn=None, close=True):
    """执行分号分隔的多条SQL"""
    if not sql_statements:
        return
    if conn is None:
        conn = conn_youhong(database)
    for sql in sql_statements.split(";"):
        sql = sql.strip()
        if not sql:
            continue
        cxn = conn.cursor()
        try:
            # 执行sql语句
            cxn.execute(sql)
            # 提交到数据库执行
            conn.commit()
        except Exception as e:
            print(e)
            continue
            # Rollback in case there is any error
            conn.rollback()
        # 关闭数据库连接
        cxn.close()
    if close:
        conn.close()

def fetchone_sql(sql, database, conn=None, close=True):
    """
    查询SQL单条数据
    """
    if sql is None or sql == "":
        return
    if conn is None:
        conn = conn_youhong(database)
    cursor = conn.cursor()
    cursor.execute(sql)
    fetchone = cursor.fetchone()
    cursor.close()
    if close:
        conn.close()
    return fetchone


def fetchall_to_dict(sql, database, conn=None, close=True):
    """返回字典对象结果"""

    if not sql:
        return
    if conn is None:
        conn = conn_youhong(database)
    cursor = conn.cursor()
    cursor.execute(sql)
    object_list = dictfetchall(cursor)
    cursor.close()
    if close:
        conn.close()
    return object_list


def exec_sql(sql, dushibaoliao, conn=None, close=True):
    """执行更新操作SQL"""
    if not sql:
        return
    if conn is None:
        conn = conn_youhong(dushibaoliao)
    cxn = conn.cursor()
    cxn.execute(sql)
    conn.commit()
    cxn.close()
    if close:
        conn.close()

def fetchall_sql(sql, dushibaoliao, conn=None, close=True):
    """返回查询结果"""
    if not sql:
        return
    if conn is None:
        conn = conn_youhong(dushibaoliao)
    cursor = conn.cursor()
    cursor.execute(sql)
    object_list = cursor.fetchall()
    cursor.close()
    if close:
        conn.close()
    return object_list

def dictfetchall(cursor):
    """Returns all rows from a cursor as a dict"""
    desc = cursor.description
    return [
        dict(zip([col[0] for col in desc], row))
        for row in cursor.fetchall()
    ]


def is_number(s):
    """
    判断字符串是否全是数字
    :param s:
    :return:
    """
    try:
        float(s)
        return True
    except ValueError:
        pass

    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass

    return False

def del_iqiyi_video(task_id, database):
    sql = """DELETE FROM iqiyi WHERE gask_id={task_id}""".format(task_id=task_id)
    exec_sql(sql, database)  # 提交删除

